<?php

namespace app\admin\controller;

use app\admin\model\ZoneList;
use app\common\controller\Backend;
use GuzzleHttp\Client;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\Model;
use think\response\Json;

class Taskfans extends Backend
{
    /**
     * Anchor模型对象
     * @var \app\admin\model\Anchor
     */
    protected $model = null;
    protected $zone = null;
    protected $taskModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\TaskFans();

        $this->zone = new ZoneList();

        $zoneList = $this->zone->all();
        $zoneData = [];
        foreach ($zoneList as $z) {
            $zoneData[$z->zone] = $z->zonename;
        }

        $this->assignconfig("zoneList", $zoneData);
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $where = json_decode($this->request->get("filter", ''), true);
        if (isset($where['zone_list.zonename']) && !empty($where['zone_list.zonename'])) {
            $where['zone_list.zone'] = $where['zone_list.zonename'];
            unset($where['zone_list.zonename']);
        }

        if (isset($where['taskBeginTime']) && !empty($where['taskBeginTime'])) {
            $time = explode(" - ", $where['taskBeginTime']);
            $where['taskBeginTime'] = ['between', [$time[0], $time[1]]];
        }

        $list = $this->model
            ->where($where)
            ->with("zoneList")
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function addjob()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        //设置过滤方法
        $params = $this->request->post('row/a');
        $must_param = [
            'livestreamerUser',
            'currentFans',
            'taskBeginTime',
            'taskKeepDate',
            'taskFansNum',
        ];

        foreach ($must_param as $v) {
            if ($v != 'currentFans') {
                if (!isset($params[$v]) || empty($params[$v])) {
                    $this->error($v . '-是必传参数');
                }
            }
        }
        $procedure_name = 'task_fans_add';
        $rs = $this->model->procedure_data_add($params, $must_param, $procedure_name);
        if ($rs['res']) {
            $this->success();
        } else {
            $this->error("操作失败");
        }
    }

    public function searchUser()
    {
        $id = request()->get('id');

        $user = Db::table("user")->where([
            'username' => $id
        ])->find();

        if (!$user)
            $this->error("没有找到用户");

        $data = Db::table('user_right')->where([
            'livestreamerUser' => $id
        ])->find();

        if (!$data)
            $this->error("没有找到用户");



        $this->success("操作成功", null, [
            'grade' => $data['grade'],
            'fansSurplusNum' => $data['fansSurplusNum'],
            'fansNum' => $user['fansNum'],
        ]);
    }
}